<?php
require $_SERVER['DOCUMENT_ROOT'] . '/tabp/modules/DBObject.php';
/*
 * Page
 *
 * @author Timo Prüße, Andre Biel
 */
class Page extends DBObject
{
    private $content, $url;
    private $author_id, $lastEditor_id; 
    private $creationDate, $lastEdit, $published;
    private $section_id, $category_id;
    
    public function __construct($titel, $content, $url, $author_id, $published = false) 
    {
        $this->setName($titel);
        $this->setContent($content);
        $this->setURL($url);
        $this->setAuthorID($author_id);
        $this->isPublished($published);
    }
    
    public function setContent($content)
    {
        if (is_string($content))
        {
            $this->content = $content;
        }
    }
    
    public function getContent()
    {
        return $this->content;
    }
    
    public function setURL($url)
    {
        if (is_string($url))
        {
            $this->url = $url;
        }
    }
    
    public function getURL()
    {
        return $this->url;
    }
    
    public function setAuthorID($author_id)
    {
        if (is_integer($author_id))
        {
            $this->author_id = $author_id;
        }
    }
    
    public function getAuthorID()
    {
        return $this->author_id;
    }
    
    public function setLastEditor($editor_id)
    {
        $this->lastEditor_id = $editor_id;
    }
    
    public function getLastEditor()
    {
        return $this->lastEditor_id;
    }
    
    public function setCreationDate($date)
    {
        if (is_integer($date))
        {
            $this->creationDate = $date;
        }
    }
    
    public function getCreationDate()
    {
        return $this->creationDate;
    }
    
    public function setLastEdit($date)
    {
        $this->lastEdit = $date;
    }
    
    public function getLastEdit()
    {
        return $this->lastEdit;
    }


    public function isPublished($published = null)
    {
        if ($published == null)
        {
            return $this->published;
        }
        else if(is_integer($published))
        {
            $this->published = $published;
        }
    }
    
    public function setSection($section_id)
    {
       if (is_integer($section_id))
       {
           $this->section_id = $section_id;
       }
    }
    
    public function getSection()
    {
        return $this->section_id;
    }
    
        public function setCategory($category_id)
    {
       if (is_integer($category_id))
       {
           $this->category_id = $category_id;
       }
    }
    
    public function getCategory()
    {
        return $this->category_id;
    }
    
    // Hinzufügen der Seite zur DB.
    public function createObject()
    {
        $statement = 'INSERT INTO tabp_pages (Name, Author_ID, URL, CreationDate) VALUES(\'' . 
                $this->getName() . '\', \'' . $this->getAuthorID() . ' \', \'' .
                $this->getURL() . '\', \'' . $this->getCreationDate() . '\')';
        
        require_once $_SERVER['DOCUMENT_ROOT'] . '/tabp/modules/DatabaseManager/DatabaseManager.php';  
        $dbMan = new DatabaseManager();
        $dbMan->connect();
        $dbMan->execSQL($statement);
        $dbMan->disconnect();
    }

    // Speichern der Seiteninformationen in der DB.
    public function saveObject()
    {        
        $statement = 'UPDATE tabp_pages SET Name = \'' . $this->getName() .
                '\', EditedBy_ID = \'' . $this->getLastEditor() . 
                '\', LastEdit = \'' . $this->getLastEdit() . 
                '\' WHERE ID_Page = \'' . $this->getID() . '\'';
           
           require_once $_SERVER['DOCUMENT_ROOT'] . '/tabp/modules/DatabaseManager/DatabaseManager.php';  
           $dbMan = new DatabaseManager();
           $dbMan->connect();
           $dbMan->execSQL($statement);
           $dbMan->disconnect();
    }

    // Laden der Seiteninformationen aus der DB.
    public function loadObjectByID($id) 
    {
        $statement = 'SELECT * FROM tabp_pages WHERE ID_Page = ' . 
                mysql_real_escape_string($id);
        
        require_once $_SERVER['DOCUMENT_ROOT'] . '/tabp/modules/DatabaseManager/DatabaseManager.php';   
        $dbMan = new DatabaseManager();
        $dbMan->connect();
        $dbMan->execSQL($statement);
        $dbMan->disconnect();
    }
}
?>